package com.ifcar.app.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.activity.R;

public class CollectArticleAdapter extends BaseAdapter {

	private List<Map<String, Object>> mList;
	private Context mContext;
	private boolean mIsSee;
	private CheckBox mGlobal;
	
	private SQLiteDatabase db;
	private MyApplication app;
	private ThemeSettingsHelper mThemeSettingsHelper;
	private LayoutInflater mInflater;
	
	private int mSelectNum;
	private boolean allCheckStatus;

	/**
	 * 收藏适配器
	 * 
	 * @param context
	 * @param list
	 * @param isSee
	 */
	public CollectArticleAdapter(Context context, List<Map<String, Object>> list, 
			boolean isSee ,CheckBox global) {
		mContext = context;
		mList = list;
		mIsSee = isSee;
		mGlobal = global;
		app = (MyApplication)mContext.getApplicationContext();
		db = app.getSQLiteDatabase();
		mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
		mInflater = LayoutInflater.from(context);
	}

	/**
	 * 收藏适配器
	 * 
	 * @param context
	 * @param list
	 * @param isSee
	 */
	public CollectArticleAdapter(Context context,
			List<Map<String, Object>> list, boolean isSee) {
		mContext = context;
		mList = list;
		mIsSee = isSee;
		mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
		mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mList.size()>0)
			map = mList.get(position);
		final String deleteID = StringUtil.GetString(map.get(NewsSQLiteOpenHelper.COLLECT_AUTOID));

		RelativeLayout view = null;

		if (convertView == null) {
		    view = (RelativeLayout)mInflater.inflate(R.layout.collect_item, null);
		    if (!mIsSee) {
		        TextView text = (TextView) view.findViewById(R.id.collect_text_title);    
		        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)text.getLayoutParams();
		        lp.addRule(RelativeLayout.LEFT_OF, R.id.collect_title_checkbox);
		        CheckBox cb = (CheckBox) view.findViewById(R.id.collect_title_checkbox);
		        cb.setVisibility(View.VISIBLE);   
		    }
		} else {
			view = (RelativeLayout) convertView;
		}
		
		TextView text = (TextView) view.findViewById(R.id.collect_text_title);	
		CheckBox cb = (CheckBox) view.findViewById(R.id.collect_title_checkbox);
		text.setTag(deleteID);
		cb.setTag(deleteID);
		cb.setChecked(false);

		if (mIsSee) {
		} else {
			
			if(allCheckStatus) cb.setChecked(true);
			else {
				
	
				String s = "select * from " + NewsSQLiteOpenHelper.COLLECT_TABLE + 
				" where " + NewsSQLiteOpenHelper.COLLECT_AUTOID + "='" + deleteID + "'";
				Cursor c = db.rawQuery(s, new String[]{});
				if(c != null && c.getCount() > 0 && c.moveToFirst() 
						&& c.getString(c.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_ISCHECKED)) != null
						&& c.getString(c.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_ISCHECKED)).equals("1")) {
					cb.setChecked(true);
				} else {
					cb.setChecked(false);
				}
				if(c!=null)	 c.close();
			
			}
				
			cb.setOnClickListener(new CheckBox.OnClickListener() {

				@Override
				public void onClick(View v) {
					
					CheckBox b = (CheckBox) v;
					
					ContentValues initialValues = new ContentValues();
					if(b.isChecked()) {
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "1");
						mSelectNum ++;
					} else {
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "0");
						mSelectNum --;
					}
					db.update(NewsSQLiteOpenHelper.COLLECT_TABLE, initialValues, "auto_id=?", new String[]{deleteID});
					
					if(mSelectNum == getCount()){
					    mGlobal.setChecked(true);
					    allCheckStatus = true;
					}else{
					    mGlobal.setChecked(false);
					    allCheckStatus = false;
					}
					
				}
			});
			
		}
		
		Object title = map.get(NewsSQLiteOpenHelper.COLLECT_TITLE);
		String str = null==title?"":title.toString();
		text.setText(str);
		String type = map.get(NewsSQLiteOpenHelper.COLLECT_TYPE) + "";
		text.setTag(type);
		mThemeSettingsHelper.setTextViewColor(mContext, text, R.color.list_title_color);
		return view;
	}
	
	public void setCheckOrNoAllBox(boolean isChecked) {
		allCheckStatus = isChecked;
		if(isChecked){
			mSelectNum = getCount();
		}else{
			mSelectNum = 0;
		}
	}
	
	public int getSelectNums() {
		return mSelectNum;
	}

}
